const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
    // 创建可读流
    const readStream = fs.createReadStream('data.txt');
    
    // 设置响应头
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    
    // 使用管道将文件流直接传输到响应流
    readStream.pipe(res);
});

server.listen(8081, () => {
    console.log('服务器运行在 http://localhost:8081');
});